$(function() {
        	
	var service = new EbeimService();
	var op = $(".form-group");
	var form = $("#j-form");
	var jbindDevice = $("#j-bindDevice");
	var jbindDeviceName = $("#j-bindDeviceName");
	
	op.each(function() {
        var t = $(this),
            input = t.find(".required"),
            name = input.attr("name"),
            holder = input.siblings(".message");
        input.blur(function() {
            activeCheck(form,name);
        }).focus(function() {
            holder.show();
        });;
    });
	
	service.getComputerMessage(function(json){
		if(json.code == 200) {
			jbindDevice.html(json.bindDevice);
			jbindDevice.siblings("[name='bindDevice']").val(json.bindDevice);
			jbindDeviceName.html(json.bindDeviceName);
			jbindDeviceName.siblings("[name='bindDeviceName']").val(json.bindDeviceName);
		}else{
            alert(json.desc);
        }
	})
	
	form.delegate(".j-getCode","click",function(){
		var data = {
			token:"tokennawang",
			username: GetQueryString("username")
			
		}
		var t = $(this);
    	t.attr("disabled", "disabled").removeClass("codeBtn").addClass("disableBtn");
    	countDown(t,120);
    	service.getMessageCode(data,function(json){
			if(json.code == 200) {
				alert(json.desc);
			}else{
                alert(json.desc);
            }
		})	
    });
	form.bind("submit",function () {
        if (!activeCheck(form)) {
        	return false;
        }else{
        	var labType = GetQueryString("labType");
            var data = form.serialize();
            
            data = data + "&username="+ GetQueryString("username");
            
            if(labType == "1"){
            	service.keyNoBindCheck(data, function(json){
                    if(json.code == 200) {
                        alert(json.desc);
                        window.location.href = "../index.html";
                    }else{
                    	alert(json.desc);
                    }
                });
            }else{
            	service.authorizationCodeNoBindCheck(data, function(json){
                    if(json.code == 200) {
                        alert(json.desc);
                        window.location.href = "../index.html";
                    }else{
                    	alert(json.desc);
                    }
                });
            }
            
        }
    })
});


//激活验证
var activeCheck = function(form,name) {

	// 显示错误信息
    var showError = function(obj, msg) {
        var msgDom = obj.siblings('.message');
        var msgTxt = msgDom.children('i')
        msgDom.addClass("msg-error").removeClass('msg-tip');
        msgTxt.html(msg);
        msgDom.show();
    };

    // 隐藏错误信息
    var hideError = function(obj, msg) {
        var msgDom = obj.siblings('.message');
        var msgTxt = msgDom.children('i')
        msgDom.hide();
        msgDom.addClass("msg-tip").removeClass('msg-error');
        msgTxt.html(msg);
    };

    // 数据验证对象
    var dataCheck = {
        code: {
            obj: form.find("[name='code']"),
            fn: function() {
                var val = this.obj.val();
                if (formCheck.isNull(val)) {
                    showError(this.obj,"请输入验证码");
                    return false;
                } else {
                    hideError(this.obj,"请输入验证码");
                    return true;
                }
            }
        }
    };

    if (name && dataCheck[name]) {
        return dataCheck[name].fn();
    } else {
        var errLen = 0;
        for (var i in dataCheck) {
            if (!dataCheck[i].fn()) {
                errLen++;
            }
        }
        if (errLen > 0) {
            return false;
        }
        return true;
    }
}
//记录定时器
var timer = "";
var countDown = function(dom,time){
	dom.val(time+"秒");
	timer = setInterval(function(){
		if(time>0){
			time--;
    		dom.val(time+"秒");
		}else{
			clearInterval(timer);
			dom.removeAttr("disabled").removeClass("disableBtn").addClass("codeBtn").val("发送验证码");
		}
	},1000);
}